package com.realshare.bby.service.integral;

import com.realshare.frame.component.quartz.base.AutoScheduler;
import com.realshare.frame.component.quartz.base.BaseScheduler;
import com.realshare.frame.util.DateUtils;
import com.realshare.frame.util.EasyHashMap;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 积分补贴账号校验
 * author leisure
 * email 15075107120@139.com
 * create 2018-11-23 下午 5:54
 * modify 2018-11-23 下午 5:54
 **/
@Service
//自动注入定时任务，并指定任务名称
@AutoScheduler(name = "AutoAccountCheckScheduler", group = "Integral")
//使用@DisallowConcurrentExecution设置不并发执行
@DisallowConcurrentExecution
public class AutoAccountCheckScheduler extends BaseScheduler {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SQLManager sqlManager;

    public AutoAccountCheckScheduler() {
        super(true);
    }

    @Override
    public String setDescription() {
        return "积分补贴账号校验任务";
    }

    @Override
    protected void executeInternal(JobExecutionContext context) {

        //查询两天前未校验账号的积分补贴记录列表
        log.info("开始处理未校验账号的积分补贴记录列表");
        String sql = "SELECT ID,IDENTITY_CODE,ID_NO FROM RULE_INTEGRAL_TASK " +
                "WHERE INTEGRAL_TYPE = 2 AND HANDLER_STATUS =10  AND ID_NO IS NOT NULL  " +
                "and SUBSTR(create_time ,1, 10)<=date_sub(curdate(),interval 2 day) order by CREATE_TIME  ";
        List<EasyHashMap> mapList = sqlManager.execute(new SQLReady(sql), EasyHashMap.class);
        String now = DateUtils.formatLongDate(new Date());
        String upSql;
        for (EasyHashMap map : mapList) {
            String id = map.getString("ID");//积分补贴id
            String identity_code = map.getString("IDENTITY_CODE").trim();//用户身份证号
            String account = map.getString("ID_NO").trim();//用户账号
            //校验是否重复录入
            sql = "select *  FROM RULE_INTEGRAL_TASK WHERE INTEGRAL_TYPE = 2 " +
                    "and HANDLER_STATUS = 11 and ID_NO = '" + account + "'";
            List<EasyHashMap> taskList = sqlManager.execute(new SQLReady(sql), EasyHashMap.class);
            if (taskList.size() > 0) {//录入账号重复
                upSql = "update RULE_INTEGRAL_TASK set HANDLER_STATUS = 13 where ID= '" + id + "'";
                log.info("服务系统校验积分补贴账号---" + account + "---重复录入");
                sqlManager.executeUpdate(new SQLReady(upSql));
            } else {//校验是否存在
                //内部账号
                sql = "SELECT IDENT_NO,CUST_NAME FROM m_ci_customer WHERE CUST_ID IN ( " +
                        "SELECT COD_CUST_NATL_ID FROM tf_mdm_ac_rel WHERE ACCT_ID IN ( " +
                        "SELECT ACCT_ID FROM TF_MDM_FCRAC_REL WHERE COD_MC_ACCT_NO IN ( " +
                        "SELECT COD_MC_ACCT_NO FROM MC_ACCT_XREF " +
                        "WHERE COD_ACCT_NO='" + account + "'))) and  IDENT_NO = '" + identity_code + "' ";
                List<EasyHashMap> insideList = sqlManager.execute(new SQLReady(sql), EasyHashMap.class);
                //外部账号
                sql = "select IDENT_NO,CUST_NAME from m_ci_customer where CUST_ID in(" +
                        "select COD_CUST_NATL_ID from tf_mdm_ac_rel " +
                        "where ACCT_NO='" + account + "') and IDENT_NO = '" + identity_code + "' ";
                List<EasyHashMap> outsideList = sqlManager.execute(new SQLReady(sql), EasyHashMap.class);
                if (insideList.size() > 0 || outsideList.size() > 0) {//账号存在
                    upSql = "update RULE_INTEGRAL_TASK set HANDLER_STATUS = 11 where ID= '" + id + "'";
                    log.info("服务系统校验积分补贴账号---" + account + "---核验通过");
                    sqlManager.executeUpdate(new SQLReady(upSql));
                } else {//账号不存在
                    upSql = "update RULE_INTEGRAL_TASK set HANDLER_STATUS = 12 where ID= '" + id + "'";
                    log.info("服务系统校验积分补贴账号---" + account + "---账号不存在");
                    sqlManager.executeUpdate(new SQLReady(upSql));
                }
            }

        }
        log.info("结束处理积分补贴账号校验任务");
    }

}